/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package others;

import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Comarch
 */
public class Serializator{
    public static String serializ(Object o){
        try{
            ByteArrayOutputStream Btab = new ByteArrayOutputStream();
            ObjectOutputStream obstream = new ObjectOutputStream( Btab );
            obstream.writeObject( o );
            obstream.close();
            return new String( Base64Coder.encode( Btab.toByteArray() ) );
        } catch(Exception e){System.out.println(e); return null;}  
    }
    //#######################################################################################
    public static Object deserialize(String s){
        try{
        byte [] btab = Base64Coder.decode( s );
        ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(btab));
        Object o  = input.readObject();
        input.close();
        return o;
        } catch(Exception e) {System.out.println(e); return null;}
    }
    //#######################################################################################
      public static void saveObject(Object o, String path){
        ObjectOutputStream out;
        try {
             out = new ObjectOutputStream(new FileOutputStream(path));
             out.writeObject(o);
             out.close();
            
        } catch (IOException ex) {
        }
    }
    //#######################################################################################  
      public static Object loadObject(String path){
        ObjectInputStream in;
        try {
             in = new ObjectInputStream(new FileInputStream(path));
             Object wyn =  in.readObject();
             in.close();
             return wyn;
        } catch (IOException | ClassNotFoundException ex) {
           System.out.println(ex);
        }
       return null;
    }
    
}
